import Vue from 'vue'
import VueRouter from 'vue-router'
// import { component } from 'vue/types/umd'

Vue.use(VueRouter)

const Login = ()=>import('@/views/Login')
const Home = ()=>import('@/views/Home')
const Welcome = ()=>import('@/components/Welcome')
const Users = ()=>import('@/components/Users')
const Rights = ()=>import('@/components/Rights')
const Roles = ()=>import('@/components/Roles')
const Categories = ()=>import('@/components/Categories')

const routes = [
  {
    path: '/',
    redirect:'/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    redirect:'/welcome',
    name: 'Home',
    component: Home,
    children:[
      {
      path:'/welcome',
      name:'Welcome',
      component:Welcome
      },
      {
        path:'/users',
        name:'Users',
        component:Users
      },
      {
        path:'/rights',
        name:'Rights',
        component:Rights
      },
      {
        path:'/roles',
        name:'Roles',
        component:Roles
      },
      {
        path:'/categories',
        name:'Categories',
        component:Categories
      }
    ]
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//登陆导航守卫
router.beforeEach(function (to, from, next) {
  if (to.path === '/home') {
    let token = sessionStorage.getItem('token')
    if(!token){
      next('/login')
    }
    next()
  }
  next()
})

export default router
